<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Urlrule.php 76892 2019-01-25 15:28:06 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_urlrule
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\response;

class urlrule extends base {

    // 首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('urlrule')->where($search['sql'])->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('urlrule/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            if (!preg_match('/^([a-zA-Z0-9]|[\/\(\)\\\+\-~!@_]){0,30}$/', $data['urlrule'])) {
                showmsg('URL规则不符合规范', '/admin/urlrule', 100);
            }
            $r = db('urlrule')->field('urlrule')->where(array('urlrule' => $data['urlrule']))->find();
            if ($r) {
                showmsg('URL规则已存在', '/admin/urlrule', 100);
            }
            db('urlrule')->insert($data);
            showmsg('添加成功', '/admin/urlrule', 200);
        } else {
            $this->assign('postUrl', '/admin/urlrule/add');
            $this->assign('action', '添加');
            $this->display('urlrule/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            if (!preg_match('/^([a-zA-Z0-9]|[\/\(\)\\\+\-~!@_]){0,30}$/', $data['urlrule'])) {
                showmsg('URL规则不符合规范', '/admin/urlrule');
            }
            $id = request::post('urlruleid', 0);
            db('urlrule')->where(['urlruleid' => $id])->update($data);
            showmsg('修改成功', '/admin/urlrule');
        } else {
            $urlruleid = request::get('id', 0);
            $item = db('urlrule')->where(['urlruleid' => $urlruleid])->find();
            $this->assign('postUrl', '/admin/urlrule/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->display('urlrule/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('urlrule')->delete(['urlruleid' => $id]);
        showmsg('删除成功', '/admin/urlrule');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('urlrule')->delete(['urlruleid' => $id]);
            }
            showmsg('删除成功', '/admin/urlrule');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';
        if ($urlruleid = trim($search['urlruleid'])) {
            $where .= "and urlruleid = '{$urlruleid}' ";
        }
        if ($name = trim($search['name'])) {
            $where .= "and name = '{$name}' ";
        }
        if ($urlrule = trim($search['urlrule'])) {
            $where .= "and urlrule = '{$urlrule}' ";
        }
        if ($route = trim($search['route'])) {
            $where .= "and route = '{$route}' ";
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['name'] = request::post('name', '');
        $arr['urlrule'] = request::post('urlrule', '');
        $arr['route'] = request::post('route', '');
        return $arr;
    }

}
